

enum MissionStatus {


    UNACCEPTABLE = 0,

    CAN_ACCEPT = 1,

    DURGING = 2,

    CAN_SUBMIT = 3,

    FINISHIED = 4
}

class Mission {

    id = 0;

    title = '';

    canAcceptContent: string = "";

    canSubmitContent: string = "";


    needLevel = 0;

    needAbility: string;
    needAttack: number;

    fromNpcId = 0;

    toNpcId = 0;

    isAccepted = false;

    isSubmit = false;

    during = false;

    current = 0;

    total = 1;

    rewards: { type: string, num: number }[] = [];

    needItem = "";
    type = "";


    status: MissionStatus = 0;


    parseFromConfig(config: any) {
        this.id = config.id;
        this.title = config.title;
        this.canAcceptContent = config.canAcceptContent
        this.canSubmitContent = config.canSubmitContent;
        this.needLevel = config.needLevel;
        this.fromNpcId = config.fromNpcId;
        this.toNpcId = config.toNpcId;
        this.rewards = config.rewards;
        this.needItem = config.needItem;
        this.type = config.type;

    }






    update() {
        let nextStatus: MissionStatus = 0;
        if (this.isSubmit) {
            nextStatus = MissionStatus.FINISHIED
        }
        else if (this.isAccepted) {
            if (this.current >= this.total) {
                nextStatus = MissionStatus.CAN_SUBMIT;
            }
            else {
                nextStatus = MissionStatus.DURGING;
            }

        }
        else if (this.during) {
            if (this.current >= this.total) {
                nextStatus = MissionStatus.CAN_SUBMIT;
            }
        }
        else {
            if (this.needLevel > user.level || this.needAttack > user.attack) {
                nextStatus = MissionStatus.UNACCEPTABLE;
            }
            else {
                nextStatus = MissionStatus.CAN_ACCEPT;
            }

        }


        if (nextStatus != this.status) {
            this.status = nextStatus;
            return true;
        }
        else {
            return false;
        }

    }
}


class MissionManager extends EventDispatcher {


    missions: Mission[] = [];

    constructor() {
        super();
    }

    acceptMission(mission: Mission) {
        mission.isAccepted = true;
        if (mission.type == "talk") {
            mission.current = 1;
        }

        if (mission.type == "item") {
            if (user.packagedItems[0].name == "刀") {
                mission.current = 1;
            }
        }

        this.update();
    }

    parseFromConfig(config: any) {
        for (let item of config.tasks) {
            const mission1 = new Mission();
            mission1.parseFromConfig(item);
            missionManager.missions.push(mission1);
        }
    }

    submitMission(mission: Mission) {
        mission.isSubmit = true;

        this.update();
        const rewards = mission.rewards;
        for (let reward of rewards) {
            if (reward.type == 'level') {
                user.level += reward.num;
            }
            else if (reward.type == 'sliverCoin') {
                user.sliverCoin += reward.num;
            }
        }
    }

    init() {
        user.addEventListener("userchange", () => {
            this.update();
        });
        this.update();
    }

    update() {
        let needUpdate = false;
        for (let mission of this.missions) {
            const missionNeedUpdate = mission.update();
            if (missionNeedUpdate) {
                needUpdate = true;
            }
        }
        if (needUpdate) {
            this.dispatchEvent("missionUpdate", {});
        }
    }


}


const missionManager = new MissionManager();